package com.woniuxy.book.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.book.controller.form.bookinfo.AddBookInfoForm;
import com.woniuxy.book.controller.form.bookinfo.SearchBookInfosForm;
import com.woniuxy.book.controller.form.bookinfo.UpdateBookInfoForm;
import com.woniuxy.book.entity.PageInfo;
import com.woniuxy.book.param.bookinfo.AddBookInfoParam;
import com.woniuxy.book.param.bookinfo.SearchBookInfosParam;
import com.woniuxy.book.param.bookinfo.UpdateBookImageParam;
import com.woniuxy.book.param.bookinfo.UpdateBookInfoParam;
import com.woniuxy.book.service.BookInfoService;
import com.woniuxy.book.service.dto.BookInfoDTO;
import com.woniuxy.book.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author li
 * @since 2024年2月09日
 */
@RestController
@RequestMapping("/bookInfo")
@Api(tags = "书籍信息相关接口")
public class BookInfoController {

    @Resource
    private OSSUtil ossUtil;

    @Resource
    private BookInfoService bookInfoService;

    //上传图书封面
    @ApiImplicitParam(name = "image",value = "封面图片对象")
    @PostMapping("/admin/uploadBookImage")
    @ApiOperation("上传书籍封面")
    public Result uploadBookImage(MultipartFile image) throws Exception{
        //1取出image中的文件内容
        InputStream inputStream = image.getInputStream();
        byte[] header = new byte[8];
        inputStream.read(header,0,8);
        //2判断该文件的类型是否是图片
        if (!FileUtil.isJpg(header)||FileUtil.isPng(header)) {
            throw new Exception("仅仅支持jpg和png图片");
        }
        //生成新的图片名称
        String prefix = UUID.randomUUID().toString().replaceAll("-","");
        String fileName = prefix+"."+(FileUtil.isJpg(header)?"jpg":"png");
        //调用API上传
        String path = ossUtil.uploadFile(image.getInputStream(), fileName, "liucunyi");
        return Result.success("上传图书封面成功",path);
    }

    //添加图书信息
    @PostMapping("/admin/addBookInfo")
    @ApiOperation("新增书籍信息")
    public Result addBookInfo(@RequestBody AddBookInfoForm form){
        bookInfoService.addBookInfo(BeanUtil.toBean(form, AddBookInfoParam.class));
        return Result.success("新增图书信息成功",null);
    }

    //修改图书封面
    @ApiImplicitParams({
            @ApiImplicitParam(name = "image",value = "封面图片对象"),
            @ApiImplicitParam(name = "bookId",value = "图书id")
    })
    @PostMapping("/admin/updateBookImage")
    @ApiOperation("修改书籍封面")
    public Result updateBookImage(MultipartFile image,Integer bookId) throws Exception{
        //1取出image中的文件内容
        InputStream inputStream = image.getInputStream();
        byte[] header = new byte[8];
        inputStream.read(header,0,8);
        //2判断该文件的类型是否是图片
        if (!FileUtil.isJpg(header)||FileUtil.isPng(header)) {
            throw new Exception("仅仅支持jpg和png图片");
        }
        //生成新的图片名称
        String prefix = UUID.randomUUID().toString().replaceAll("-","");
        String fileName = prefix+"."+(FileUtil.isJpg(header)?"jpg":"png");
        UpdateBookImageParam param = new UpdateBookImageParam();
        param.setBookId(bookId);
        param.setFileName(fileName);
        param.setImage(image);
        bookInfoService.updateBookImage(param);
        return Result.success("更换图书封面成功",null);
    }

    //分页条件搜索
    @PostMapping("/admin/searchBookInfos")
    @ApiOperation("分页查询图书信息")
    public Result searchBookInfos(@RequestBody SearchBookInfosForm form){
        PageInfo pageInfo = bookInfoService.searchBookInfos(BeanUtil.toBean(form, SearchBookInfosParam.class));
        return Result.success("查询图书信息成功",pageInfo);
    }

    @PostMapping("/user/selectBookInfos")
    public Result selectBookInfos(@RequestBody SearchBookInfosForm form,@RequestHeader("token")String token) throws Exception {
        PageInfo pageInfo = bookInfoService.selectBookInfos(BeanUtil.toBean(form, SearchBookInfosParam.class),token);
        return Result.success("查询图书信息成功",pageInfo);
    }

    @GetMapping("/user/findBookInfoById")
    public Result findBookInfoById(Integer bookId,@RequestHeader("token")String token){
        BookInfoDTO bookInfoDTO = bookInfoService.findBookInfoById(bookId,token);
        return Result.success("单独书籍信息查询成功",bookInfoDTO);
    }

    //修改图书信息
    @PostMapping("/admin/updateBookInfo")
    @ApiOperation("修改图书信息")
    @RequiredRole(value = "管理员")
    public Result updateBookInfo(@RequestBody UpdateBookInfoForm form){
        bookInfoService.updateBookInfo(BeanUtil.toBean(form, UpdateBookInfoParam.class));
        return Result.success("修改图书信息成功",null);
    }

    //下架图书
    @ApiImplicitParam(name = "bookId",value = "图书id")
    @GetMapping("/admin/disableBookInfo")
    @ApiOperation("下架图书")
    public Result disableBookInfo(Integer bookId){
        bookInfoService.updateBookState(bookId, StateData.BOOK_STATE_DISABLED);
        return Result.success("下架图书成功",null);
    }

    //上架图书
    @ApiImplicitParam(name = "bookId",value = "图书id")
    @GetMapping("/admin/enableBookInfo")
    @ApiOperation("上架图书")
    public Result enableBookInfo(Integer bookId){
        bookInfoService.updateBookState(bookId, StateData.BOOK_STATE_NORMAL);
        return Result.success("上架图书成功",null);
    }

    //删除图书
    @ApiImplicitParam(name = "bookId",value = "图书id")
    @GetMapping("/admin/deleteBookInfo")
    @ApiOperation("删除图书")
    public Result deleteBookInfo(Integer bookId){
        bookInfoService.deleteBookInfo(bookId);
        return Result.success("删除图书成功",null);
    }

    //撤回删除
    @ApiImplicitParam(name = "bookId",value = "图书id")
    @GetMapping("/admin/undeleteBookInfo")
    @ApiOperation("撤回图书删除")
    public Result undeleteBookInfo(Integer bookId){
        bookInfoService.undeleteBookInfo(bookId);
        return Result.success("撤销删除成功",null);
    }

    @ApiImplicitParam(name = "ids",value = "图书id字符串（用,分隔）")
    @GetMapping("/admin/delBookInfoBySelect")
    @ApiOperation("批量删除图书信息")
    public Result delBookInfoBySelect(String ids){
        //切割
        String[] bookIdsArray = ids.split(",");
        bookInfoService.delBookInfoBySelect(bookIdsArray);
        return Result.success("批量删除成功",null);
    }

    @ApiImplicitParam(name = "ids",value = "图书id字符串，（用，分隔）")
    @GetMapping("/admin/undelBySelect")
    @ApiOperation("批量撤回删除的图书")
    public Result undelBySelect(String ids){
        //切割
        String[] bookIdsArray = ids.split(",");
        bookInfoService.undelBySelect(bookIdsArray);
        return Result.success("批量撤销删除成功",null);
    }

    @ApiImplicitParam(name = "ids",value = "图书id字符串，（用，分隔）")
    @GetMapping("/admin/disableBySelect")
    @ApiOperation("批量下架图书")
    public Result disableBySelect(String ids){
        //切割
        String[] bookIdsArray = ids.split(",");
        bookInfoService.disableBySelect(bookIdsArray);
        return Result.success("批量下架成功",null);
    }

    @ApiImplicitParam(name = "ids",value = "图书id字符串，（用，分隔）")
    @GetMapping("/admin/enableBySelect")
    @ApiOperation("批量上架图书信息")
    public Result enableBySelect(String ids){
        //切割
        String[] bookIdsArray = ids.split(",");
        bookInfoService.enableBySelect(bookIdsArray);
        return Result.success("批量上架成功",null);
    }



}

